Skip to content

Fix #532: "@: unbound variable" when terraform run with no arguments - #541

Open
marcleblanc2 wants to merge 3 commits into
tfutils:masterfrom
marcleblanc2:fix/exec-no-args-bash3
Open

marcleblanc2 wants to merge 3 commits into
tfutils:masterfrom
marcleblanc2:fix/exec-no-args-bash3

Conversation

@marcleblanc2

@marcleblanc2 marcleblanc2 commented Sep 29, 2026 •

Copy link
Copy Markdown

Description

Running the terraform shim with no arguments fails on macOS /bin/bash (Bash 3.2):

lib/tfenv-exec.sh: line 31: @: unbound variable

Bash 3.2 treats "${@}" as unbound under set -u when there are no arguments. 0bd45d1 (#453) introduced for _arg in "${@}" to stop word-splitting arguments with spaces. zsh's bashcompinit runs complete -C commands with no arguments, so on macOS every tab completion after terraform hit this.

Fix: for _arg; do, which iterates the positional parameters without expanding "$@". Works on Bash 3.2 through 5 and keeps #453 fixed.

Also updates the for-loop guidance in AGENTS.md and .github/instructions/bash.instructions.md, which recommended the form that caused this.

Issue Link

Fixes #532

Testing

  • Ran ./test/run.sh locally; all suites pass except install_and_use, prerelease_filter, use_dash, use_latestallowed, which fail identically on master on this machine (Bad CPU type in executable: pre-1.0 darwin_amd64 builds on Apple Silicon without Rosetta)
  • Tested on at least one platform (note which below)
  • This branch passes on macos-latest and ubuntu-latest with this repo's unchanged test.yml run on a fork (run 41146231609): all 16 suites pass, including the new test_exec.sh. Note: fork PR runs on this repo currently die before any job starts ("workflow file issue"), so the checks here have not executed.
  • New tests added: test/test_exec.sh

Platform tested: macOS 26 (arm64), Bash 3.2.57 and 5.x

Quality Checklist

  • Shell quoting verified — all variables quoted, braces used
  • Cross-platform considered — BSD vs GNU tool differences
  • No unintended changes to other commands
  • README.md updated (not user-facing)
  • CHANGELOG.md updated (left for release, per AGENTS.md)

…ash 3.2 when run with no arguments

0bd45d1 changed the argument loop in tfenv-exec() to `for _arg in "${@}"`
to stop word-splitting arguments with spaces (tfutils#453). Under `set -u`,
Bash 3.2 (macOS /bin/bash) treats an empty "$@" as an unbound variable,
so running the shim with no arguments now aborts before terraform runs.

zsh's bashcompinit invokes `complete -C` commands with no arguments, so
every tab completion after `terraform` printed only this error.

Use `for _arg; do`, which iterates the positional parameters without
expanding "$@", so it neither word-splits nor trips `set -u` on any
Bash version.

Add test/test_exec.sh covering the shim with no arguments (under both
`bash` and /bin/bash) and a -chdir path containing a space, and correct
the for-loop guidance in AGENTS.md and bash.instructions.md.

Amp-Thread-ID: https://ampcode.com/threads/T-01a0eee4-d46a-776d-9873-341f0d5f7fd2
Co-authored-by: Amp <amp@ampcode.com>
@marcleblanc2

Copy link
Copy Markdown
Author

Hey @Zordrak, is this project active and / or looking for maintainers? It's been a game changer to use.

Borrowed from tfutils#539: a terraform 'version -json' case that checks arguments
reach terraform independent of -chdir handling, and chmod +x to match the
other test_*.sh files.

Amp-Thread-ID: https://ampcode.com/threads/T-01a18edd-7c03-774f-890d-1da6f2ebc8c4
Co-authored-by: Amp <amp@ampcode.com>
@marcleblanc2 marcleblanc2 changed the title Fix #532: terraform shim fails with "@: unbound variable" on Bash 3.2 when run with no arguments Fix #532: "@: unbound variable" when run with no arguments, and tab completions Oct 30, 2026
@marcleblanc2 marcleblanc2 changed the title Fix #532: "@: unbound variable" when run with no arguments, and tab completions Fix #532: "@: unbound variable" when terraform run with no arguments Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/opt/homebrew/Cellar/tfenv/3.2.2/lib/tfenv-exec.sh: line 31: @: unbound variable

1 participant